<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Model_jadwal extends CI_Model {
	
	var $table_name = array("Jadwal" => "jadwal_perjalanan", "Stasiun" => "stasiun", "Kereta" => "kereta");
	
	function __construct()
	{
		parent::__construct();
	}
	
	function get_all()
	{
		$query = $this->db->get($this->table_name);
		return $query->result();
	}
	
	function get_by_stasiun($id)
	{
		$sql = 'SELECT a.id, b.Nama_stasiun AS \'Asal\', c.Nama_stasiun AS \'Tujuan\', d.No_KRL, d.id AS id_kereta ,d.Kelas ,TIME_FORMAT(a.jam_ber,\'%h:%i %p\') as Berangkat, TIME_FORMAT(a.jam_dat,\'%h:%i %p\') AS Datang
				FROM '.$this->table_name['Jadwal'].' a,'.$this->table_name['Stasiun'].' b,'.$this->table_name['Stasiun'].' c,'.$this->table_name['Kereta'].' d 
				WHERE b.id = ? AND b.Kode = a.kd_sta AND c.Kode = a.kd_sta_next AND a.no_krl = d.No_KRL
				ORDER BY a.jam_ber ASC;';
		$query = $this->db->query($sql,array($id));
		return $query->result();
	}
	
	function get_by_kereta($id)
	{
		$sql = 'SELECT a.id, b.Nama_stasiun AS \'Asal\', c.Nama_stasiun AS \'Tujuan\', d.No_KRL, d.id AS id_kereta ,d.Kelas ,TIME_FORMAT(a.jam_ber,\'%h:%i %p\') as Berangkat, TIME_FORMAT(a.jam_dat,\'%h:%i %p\') AS Datang
				FROM '.$this->table_name['Jadwal'].' a,'.$this->table_name['Stasiun'].' b,'.$this->table_name['Stasiun'].' c,'.$this->table_name['Kereta'].' d 
				WHERE d.id = ? AND b.Kode = a.kd_sta AND c.Kode = a.kd_sta_next AND a.no_krl = d.No_KRL
				ORDER BY a.jam_ber ASC;';
		$query = $this->db->query($sql,array($id));
		return $query->result();
	}
	
}
